<?php

namespace app\index\controller;
use app\index\Controller;
use think\Db;
use think\Validate;
/**
 * @title 司机等级（模块08）
 * @description 接口说明
 * @group 用户
 * @header name:model_id   default:08 desc:模块id
 * @groupremark 已使用错误码： 1080001,1080002,1080003
 */
class DriverRank extends Controller{
    /**
     * @var array 白名单方法，如果设置会覆盖黑名单方法，只允许白名单方法能正常访问
     */
    protected static $allowList = ['index','info'];
    /**
     * @title 获取司机等级信息
     * @description 接口说明
     * @author 梁敏
     * @url /index/driver_rank/index
     * @method ~
     * @return list:司机等级信息@
     * @list name:名称 min_score:升级分数 order_num:升级订单量 parent_id:父节点 parent_name:父名称
     */
    public function index() {

        //获取数据
        $map['tr.depth'] = 1;
        $map['d.isdelete'] = false;
        $list = Db('driver_rank_tree')->alias('tr')
            ->join('tp_driver_rank d ', 'd.id=tr.son_id', 'left')
            ->join('tp_driver_rank pd ', 'pd.id=tr.parent_id', 'left')
            ->join('tp_driver_rank_tree td ', 'td.son_id=tr.son_id and td.parent_id=0', 'left')
            ->where($map)
            ->field("tr.*,d.*,pd.name as parent_name,td.depth")
            ->order("tr.left asc")
            ->select();
        if($list){
            $msg['list']=$list;
            return $this->success($msg);
        }else{
            $msg['error_id']="1080001";
            $msg['message']="不存在或未填写信息";
            return $this->error($msg);
        }

    }
    /**
     * @title 获取司机等级详情
     * @description
     * @author 梁敏
     * @url /index/driver_rank/info
     * @method ~
     * @param name:rank_id type:int require:1 default:1 other: desc:等级id
     * @return data:司机等级信息@!
     * @data name:名称 min_score:升级分数 order_num:升级订单量 parent_id:父节点 parent_name:父名称
     */
    public function info() {
        $data=input();
        $vmsg = [
            'rank_id.require'   => '未指等级id',
        ];
        $validate = new Validate([
            'rank_id'=>"require",
        ],$vmsg);

        if (!$validate->check($data)) {
            $msg['error_id']="1080002";
            $msg['message']=$validate->getError();
            return $this->error($msg);
        }

        //获取数据
        $map['tr.depth'] = 1;
        $map['d.isdelete'] = false;
        $map['d.id'] = $data['rank_id'];
        $info = Db('driver_rank_tree')->alias('tr')
            ->join('tp_driver_rank d ', 'd.id=tr.son_id', 'left')
            ->join('tp_driver_rank pd ', 'pd.id=tr.parent_id', 'left')
            ->join('tp_driver_rank_tree td ', 'td.son_id=tr.son_id and td.parent_id=0', 'left')
            ->where($map)
            ->field("tr.*,d.*,pd.name as parent_name,td.depth")
            ->order("tr.left asc")
            ->find();
        if($info){
            $msg['data']=$info;
            return $this->success($msg);
        }else{
            $msg['error_id']="1080003";
            $msg['message']="不存在或未填写信息";
            return $this->error($msg);
        }

    }
}
